/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.unesp.demac.bcc.soo.dao;

import br.unesp.demac.bcc.soo.bean.Contato;
import br.unesp.demac.bcc.soo.bean.Pessoa;
import br.unesp.demac.bcc.soo.util.FabricaConexao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author flavioam
 */
public class ContatoDAOImp implements ContatoDAO {
    
        
    
    @Override
     public Contato selecionaContadoByIdPessoa(int idPessoa) {

        Contato c = null;
        Connection con = null;

        try {

            con = FabricaConexao.getConexao();

            String sql = "select * from Contatos where Pessoas_idPessoas = ?";

            PreparedStatement pstmt = con.prepareStatement(sql);

            pstmt.setInt(1, idPessoa);

            ResultSet rs = pstmt.executeQuery();

            if (rs.next()) {

                c = new Contato();
                c.setIdContato(rs.getInt("idContatos"));
                c.setTelefoneCelular(rs.getString("telefoneCelular"));
                c.setTelefoneComercial(rs.getString("telefoneComercial"));
                c.setTelefoneResidencial(rs.getString("telefoneResidencial"));
                c.setEmail(rs.getString("email"));
                                
            }

        } catch (SQLException ex) {

            throw new Exception(ex.getMessage());

        } catch (Exception ex) {

            throw new Exception(ex.getMessage());

        } finally {

            FabricaConexao.fechar(con);
            return c;
        }
    }
   
    
    @Override
    public boolean salvaContato(Pessoa p,Connection con) {

        boolean saved = false;

        try {

            String sql = "insert into Contatos (email,telefoneResidencial,telefoneCelular,telefoneComercial,Pessoas_idPessoas) values(?,?,?,?,?)";

            PreparedStatement pstmt = con.prepareStatement(sql);
            
            Contato c = p.getContato();

            pstmt.setString(1, c.getEmail());            
            pstmt.setString(2, c.getTelefoneResidencial());
            pstmt.setString(3, c.getTelefoneCelular());
            pstmt.setString(4, c.getTelefoneComercial());
            pstmt.setInt(5, p.getIdPessoa());            

            pstmt.executeUpdate();

            saved = true;

        } catch (SQLException e) {

            throw new Exception(e.getMessage());

        } catch (Exception e) {

            throw new Exception(e.getMessage());

        } finally {
            return saved;
        }
    }
    
    @Override
    public boolean editaContato(Pessoa p,Connection con) {

        boolean saved = false;

        try {

            String sql = "update Contatos set email = ?, telefoneResidencial = ?, telefoneCelular = ?, telefoneComercial = ? where Pessoas_idPessoas = ?";
            
            PreparedStatement pstmt = con.prepareStatement(sql);
            
            Contato c = p.getContato();

            pstmt.setString(1, c.getEmail());            
            pstmt.setString(2, c.getTelefoneResidencial());
            pstmt.setString(3, c.getTelefoneCelular());
            pstmt.setString(4, c.getTelefoneComercial());
            pstmt.setInt(5, p.getIdPessoa());            

            pstmt.executeUpdate();

            saved = true;
            

        } catch (SQLException e) {

            e.printStackTrace();
            throw new Exception(e.getMessage());

        } catch (Exception e) {

            e.printStackTrace();
            throw new Exception(e.getMessage());

        } finally {
            System.out.println(saved);
            return saved;
        }
    }
}
